/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */

package com.crm.web.page;

import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.IPage;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.form.StringPropertySelectionModel;
import org.apache.tapestry.form.LabeledPropertySelectionModel;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.valid.ValidationDelegate;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.Persist;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.crm.model.Contact;
import com.crm.model.User;
import com.crm.service.IContactService;
import com.crm.web.util.Constants;
import com.javaforge.tapestry.spring.annotations.InjectSpring;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class ContactModify extends BasePage implements PageBeginRenderListener {

    private static Log log = LogFactory.getLog(ContactModify.class);
//    private String contactId;
//    private Contact contact;
    private boolean modify;

    @InjectSpring("contactService")
    public abstract IContactService getContactService();

    @InjectPage("ContactList")
    public abstract ContactList getContactListPage();

    @Bean
    public abstract ValidationDelegate getDelegate();

    @InjectState("userSession")
    public abstract User getUserSession();

    public abstract String getContactName();

    public abstract String getDepartment();

    public abstract String getJobTitle();

    public abstract String getEmail();

    public abstract String getCountryCode();

    public abstract String getAreaCode();

    public abstract String getPhone();

    public abstract String getExtension();

    public abstract String getCountryCodeFax();

    public abstract String getAreaCodeFax();

    public abstract String getFax();

    public abstract String getExtensionFax();

    public abstract String getComment();

    public abstract String getSex();

    @Persist
    public abstract Contact getContactInfo();

    public abstract void setContactInfo(Contact contactInfo);

    @Persist
    public abstract String getSourcePage();

    public abstract void setSourcePage(String sourcePage);    

    // control modify form show
    public void setModify(boolean modify) {
        this.modify = modify;
    }

    public boolean isModify() {
        return modify;
    }

    //public void modify(){}

    public String goback() {
        return getSourcePage();
    }

    // Model's value of drop-down list box
    public IPropertySelectionModel getSexModel() {
        return new LabeledPropertySelectionModel(
                new StringPropertySelectionModel(Constants.SEX_SELECT),
                Constants.DFT_SELECT_LABEL);
    }

    public void pageBeginRender(PageEvent event) {
        if (null == getUserSession() || null == getContactInfo()) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        }
        String currentLoginUser = getUserSession().getUserCode();
        String owner = getContactInfo().getUser().getUserCode();
        if (!currentLoginUser.equals(owner) || getSourcePage().equals("ContactSearch")) {
            setModify(false);
        }
        else {
            setModify(true);
        }
    }

    public String infoSubmit() {
        try {
            Contact contact = getContactInfo();

            if (getContactName() != null) contact.setContactName(getContactName());
            if (getDepartment() != null) contact.setDepartment(getDepartment());
            if (getJobTitle() != null) contact.setJobTitle(getJobTitle());
            if (getEmail() != null) contact.setEmail(getEmail());
            if (null != getSex() && !getSex().equals(contact.getSex())) contact.setSex(getSex());

            String phoneString = "";
            if (getCountryCode() != null || getAreaCode() != null || getPhone() != null || getExtension() != null) {
                if (getCountryCode() == null || getAreaCode() == null || getPhone() == null) {
                    String message = "电话号码必须填写完整！";
                    getDelegate().record(message, ValidationConstraint.CONSISTENCY);
                    return getPageName();
                } else {
                    phoneString = getCountryCode() + "-" + getAreaCode() + "-" + getPhone();
                    if (getExtension() != null) phoneString = phoneString + "-" + getExtension();
                    contact.setPhone(phoneString);
                }
            }

            String faxString = "";
            if (getCountryCodeFax() != null || getAreaCodeFax() != null || getFax() != null || getExtensionFax() != null) {
                if (getCountryCodeFax() == null || getAreaCodeFax() == null || getFax() == null) {
                    String message = "传真号码必须填写完整！";
                    getDelegate().record(message, ValidationConstraint.CONSISTENCY);
                    return getPageName();
                } else {
                    faxString = getCountryCodeFax() + "-" + getAreaCodeFax() + "-" + getFax();
                    if (getExtensionFax() != null) faxString = faxString + "-" + getExtensionFax();
                    contact.setFax(faxString);
                }
            }

            if (phoneString.length() > 32 || faxString.length() > 32) {
                getDelegate().record("电话号码或传真不能超过29个字符", ValidationConstraint.CONSISTENCY);
                return getPageName();
            }

            if (getComment() != null) contact.setComments(getComment());

            getContactService().create(contact);

            log.info(getUserSession().getUsername() + " : Modify contact! " + contact.toString());

            return "ContactModify";
        }
        catch (Exception e) {
            String exceptionString = "The exception happened when modify contact info : " + e.getMessage();
            log.warn(getUserSession().getUsername() + " : " + exceptionString);
            e.printStackTrace();
            getDelegate().record(exceptionString, ValidationConstraint.CONSISTENCY);
            return getPageName();
        }
    }
}
